package org.school.controller;

import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.school.entity.Cai;
import org.school.entity.message2;
import org.school.service.CaiService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * (Cai)表控制层
 *
 * @author makejava
 * @since 2023-01-13 17:44:31
 */
@RestController

@RequestMapping("cai")
@CrossOrigin
@Log4j2
public class CaiController {
    /**
     * 服务对象
     */
    @Resource
    private CaiService caiService;

    /**
     * 分页查询
     *
     * @param cai 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping("all")
    public ResponseEntity<PageInfo<Cai>> queryByPage(int pageNum,int size) {
        return ResponseEntity.ok(this.caiService.queryByPage(pageNum, size));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Cai> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.caiService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param cai 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Cai> add(Cai cai) {
        return ResponseEntity.ok(this.caiService.insert(cai));
    }

    /**
     * 编辑数据
     *
     * @param cai 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Cai> edit(Cai cai) {
        return ResponseEntity.ok(this.caiService.update(cai));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<message2> deleteById(@Param("shopname") String shopname,@Param("name") String name) {
        System.out.println(shopname);
        System.out.println(name);
        return ResponseEntity.ok().body(this.caiService.deleteByName(shopname,name));
    }
    @CrossOrigin
@PostMapping("shop/addCai")//添加菜品
public Cai insertCai(@RequestBody Cai c){
    return caiService.insert(c);
}
@PostMapping("shop/addCaiImage")
public message2 uploadImage(MultipartFile file){
        if(file.isEmpty()){
            return new message2("空的图片",-1);
        }
    return caiService.uploadImg(file);
}
}

